package com.atkxp.controller;

import com.atkxp.pojo.Blog;
import com.atkxp.pojo.MyPair;
import com.atkxp.pojo.User;
import com.atkxp.service.BlogService;
import com.atkxp.service.CommentService;
import com.atkxp.service.RankListService;
import com.atkxp.service.UserService;
import org.ocpsoft.prettytime.PrettyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author user
 * @declaration
 * @create 2023/1/16-0:14
 */
@Controller
@RequestMapping("ranklist")
public class RankListController {

    @Autowired
    public RankListService rankListService;

    @Autowired
    public BlogService blogService;

    @Autowired
    public UserService userService;

    @Autowired
    public CommentService commentService;

    @RequestMapping("/toRankList")
    public String toRankList(HttpServletRequest request){
        List<User> users = rankListService.rankListForUserOrderByBlogCounts();
        List<Blog> blogs1 = rankListService.rankListForBlogOrderByLikes();
        List<Blog> blogs2 = rankListService.rankListForBlogOrderByViews();
        Map<User, Long> usermap = new LinkedHashMap<>();
        for(User user : users){
            usermap.put(user, blogService.getBlogCountLimitByUserId(user.getId()));
        }
        Map<Blog, MyPair<String, MyPair<User, Integer>>> map1 = new LinkedHashMap();
        for(Blog blog : blogs1){
            map1.put(blog, new MyPair<String, MyPair<User, Integer>>(new PrettyTime().format(blog.getBlogDate()), new MyPair<User, Integer>(userService.showUserInfoByName(blog.getUserName()), commentService.getCommentNum(blog.getId()))));
        }
        Map<Blog, MyPair<String, MyPair<User, Integer>>> map2 = new LinkedHashMap();
        for(Blog blog : blogs2){
            map2.put(blog, new MyPair<String, MyPair<User, Integer>>(new PrettyTime().format(blog.getBlogDate()), new MyPair<User, Integer>(userService.showUserInfoByName(blog.getUserName()), commentService.getCommentNum(blog.getId()))));
        }
        request.setAttribute("topUser", usermap);
        request.setAttribute("topBlogByLikes", map1);
        request.setAttribute("topBlogByViews", map2);
        return "RankList";
    }
}
